package com.liangjian.mylib.utils;

import android.content.Context;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 过滤
 */
public class FilterUtils {
    //过滤表情正则表达式
    public static final String EMOJI = "[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]";
    //匹配昵称
    public static final String MATCHER_NICKNAME = "[A-Za-z0-9_\\-\\u4e00-\\u9fa5]+";

    //匹配数字、中英文、标点符号
    public static final String MATCHER_DEFAULT = "[A-Za-z0-9_\\-\\u4e00-\\u9fa5\\p{P}]+";

    //过滤金额
    public static final String MATCHER_MONEY = "(^[1-9]\\d*(\\.\\d{1,2})?$)|(^[0]{1}(\\.\\d{1,2})?$)";

    //匹配中文字符
    public static final String MATCHER_CN = "[\\u4e00-\\u9fa5]";

    /**
     * @param context
     * @return 返回EditText输入文本过滤器
     */
    public static InputFilter getInputFilter(final Context context, final String toast, final String regex) {
        InputFilter inputFilter = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence charSequence, int start, int end, Spanned dest, int dstart, int dend) {
                if (charSequence.length() > 0) {
                    if (isMatcher(charSequence.toString(), regex)) {
                        return null;
                    } else {
                        ToastUtil.showShortToast(context, toast);
                        return "";
                    }
                }
                return null;
            }
        };
        return inputFilter;
    }

    /**
     * 正则表达式匹配
     *
     * @param content 待匹配内容
     * @param regex   正则表达式
     * @return true 匹配成功
     */
    public static boolean isMatcher(String content, String regex) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(content);
        return matcher.find();
    }

    /**
     * 判断是否是手机号
     * @param mobiles：
     * @return
     */
    public static boolean isMobileNum(String mobiles) {
        //"^((13[0-9])|(14[0-9])|(15[0-9])|(16[0-9])|(17[0-9])|(18[0-9]))\\d{8}$"
        Pattern p = Pattern
                .compile("^((13)|(14)|(15)|(16)|(17)|(18)|(19))\\d{9}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    /**
     * 是否是邮箱
     * @param strEmail
     * @return
     */
    public static boolean isEmail(String strEmail) {
        String strPattern = "^[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";
        if (TextUtils.isEmpty(strPattern)) {
            return false;
        } else {
            return strEmail.matches(strPattern);
        }
    }

    /**
     * 截取手机号码 中间四位为****
     *
     * @param phonenum
     * @return
     */
    public static String subPhone(String phonenum) {
        String phone = null;
        if (!TextUtils.isEmpty(phonenum) && phonenum.length() > 6) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < phonenum.length(); i++) {
                char c = phonenum.charAt(i);
                if (i >= 3 && i <= 6) {
                    phone = sb.append('*').toString();
                } else {
                    phone = sb.append(c).toString();
                }
            }
        }
        return phone;
    }



}
